package com.common.dongqiuhui.apps.comm.net;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface.OnCancelListener;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.view.View.OnClickListener;

import com.common.dongqiuhui.apps.comm.ctr.PlayingController;
import com.common.dongqiuhui.apps.comm.tools.BallUtil;
import com.common.dongqiuhui.apps.comm.tools.ExceptionUtil;
import com.common.dongqiuhui.apps.comm.util.DataResult;

/**
 * 联网异步操作 提供了设置loading字符串,联网成功失败的回调
 * 
 * @author wangbingqi
 * 
 */
public abstract class BallAsyncTask extends AsyncTask<String, Integer, DataResult> {
	private ProgressDialog mDialog;
	protected Context context;
	protected String dialogMsg = "提交中.....";

	/**
	 * asynctask任务编号
	 * <p>
	 * <b>注意：</b>taskCode必须是大于等于0的整数,默认单个task的taskCode为0
	 * <p>
	 * <b> 当前页区分多个任务的回调，请用taskCode区分处理
	 * <p>
	 * switch (taskCode) { case 1: // logic 1 break;
	 * 
	 * case 2: // logic 2 break;
	 * 
	 * default: break; } </b>
	 */
	protected int taskCode;

	/**
	 * asynctask执行结果回调接口
	 */
	private TaskCallable taskCallable;
     /**
      * 是否需要联网异步,默认需要判断网络状态
      */
	public boolean mParentisNeedNet=true;
	public BallAsyncTask(Context context) {
		this.context = context;
	}

	/**
	 * 请求处理结果通过接口回调 使用
	 * 
	 * @param context
	 * @param taskCallable
	 */
	public BallAsyncTask(Context context, int taskCode, TaskCallable taskCallable) {
		this.context = context;
		this.taskCode = (taskCode < 0) ? 0 : taskCode;
		this.taskCallable = taskCallable;
	}

	@Override
	protected DataResult doInBackground(String... params) {
		DataResult ret = null;
		try {
			if (BallUtil.hasNetwork(context)||!mParentisNeedNet) {
				ret = toNet(params);
			} else {
				ret = new DataResult();
				ret.message = PlayingController.NetworkExceptionMessage;
				ret.status = PlayingController.NetworkExceptionCode;
			}
		} catch (Exception e) {
			ret = new DataResult();
			ret.message = PlayingController.SystemExceptionMessage;
			ret.status = PlayingController.SystemExceptionCode;
			ret.error = ExceptionUtil.printException(e).toString();
		}
		return ret;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		showDialog(null, getDialogMessage(), null, null, true, null, null);
	}

	@Override
	protected void onPostExecute(DataResult result) {
		super.onPostExecute(result);
		closeDialog();
		if (result == null) {
			return;
		}
		switch (result.status) {
		case PlayingController.SUCCESS_CODE:
			success(result);
			// 通过接口回调成功结果
			if (null != taskCallable) {
				taskCallable.success(context, taskCode, result);
			}

			break;

		default:
			failure(result);

			// 通过接口回调失败结果
			if (null != taskCallable) {
				taskCallable.success(context, taskCode, result);
			}

			break;
		}
	}

	/**
	 * 关闭对话框
	 */
	public final void closeDialog() {
		if (null!=mDialog&&mDialog.isShowing()) {
			try {
				mDialog.dismiss();
			} catch (IllegalArgumentException e) {
				// TODO: handle exception
			}
			
			mDialog = null;
		}
	}

	/**
	 * 显示对话框
	 */
	private void showDialog(String title, String content, String okText, String cancleText, boolean cancelable,
			OnCancelListener btnCancle, OnClickListener btnOk) {
		if (content == null || content.equalsIgnoreCase(""))
			return;
		try {
			if (mDialog == null) {
				mDialog = new ProgressDialog(context);
			}
			mDialog.setTitle(title);
			mDialog.setMessage(content);
			mDialog.setCancelable(cancelable);
			mDialog.show();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * 联网方法
	 * 
	 * @param params
	 * @return
	 */
	protected abstract DataResult toNet(String[] params);

	/**
	 * 联网成功
	 * 
	 * @param result
	 */
	protected abstract void success(DataResult result);

	/**
	 * 联网失败
	 * 
	 * @param result
	 */
	protected abstract void failure(DataResult result);

	/**
	 * 设置loading对话栏显示信息
	 * 
	 * @return
	 */
	protected String getDialogMessage() {
		return dialogMsg;
	}

	/**
	 * 设置对话框的message
	 * 
	 * @param msg
	 */
	public void setDialogMessage(String msg) {
		dialogMsg = TextUtils.isEmpty(msg) ? dialogMsg : msg;
	}
}
